﻿using TjZheng.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using TjZheng.Framework;

namespace TjZheng.EasyUI
{
    public class QueryCheckbox : EasyUIQueryControl
    {
        /// <summary>
        /// 未选中的值
        /// </summary>
        public string UnCheckedValue { get; set; }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="QueryField">查询字段QueryField必输</param>
        /// <param name="QueryValue">选中值</param>
        /// <param name="UnCheckedValue">未选中值</param>
        public QueryCheckbox(string QueryField, string QueryValue, string UnCheckedValue)
            : this(string.Empty, QueryField, QueryValue, UnCheckedValue)
        {
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="Prefix">Model前缀</param>
        /// <param name="QueryField">查询字段QueryField必输</param>
        /// <param name="QueryValue">选中值</param>
        /// <param name="UnCheckedValue">未选中值</param>
        public QueryCheckbox(string Prefix, string QueryField, string QueryValue, string UnCheckedValue)
            : base(Prefix, QueryField)
        {
            if (QueryValue.IsNull())
            {
                throw new Exception("选中值QueryValue不能为空");
            }
            if (UnCheckedValue.IsNull())
            {
                throw new Exception("未选中值UnCheckedValue不能为空");
            }
            this.QueryValue = QueryValue;
            this.UnCheckedValue = UnCheckedValue;
            this.QueryMethod = QueryMethod.Equal;
            this.QueryDataType = QueryDataType.Boolean;
        }
        /// <summary>
        /// 渲染查询控件
        /// </summary>
        /// <returns></returns>
        public override string RenderHtml()
        {
            TagBuilder objTagBuilder = new TagBuilder("input");
            objTagBuilder.MergeAttribute("type", "checkbox");
            objTagBuilder.MergeAttribute("name", this.Prefix.Append(".QueryValue"));
            objTagBuilder.MergeAttribute("value", this.QueryValue);
            objTagBuilder.GenerateId(this.Id);
            if (HtmlAttributes.IsNull())
            {
                HtmlAttributes = new Dictionary<string, object>();
            }
            foreach (var objKey in HtmlAttributes.Keys)
            {
                objTagBuilder.MergeAttribute(objKey, HtmlAttributes[objKey].ToString());
            }
            string objCheckbox = objTagBuilder.ToString(TagRenderMode.SelfClosing)
                .AppendFormat("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", this.Prefix.Append(".QueryValue"), this.UnCheckedValue);
            if (QueryTitle.IsNoNull())
            {
                objCheckbox = string.Format("{0}:{1}", this.QueryTitle, objCheckbox);
            }
            return objCheckbox;
        }
    }
}
